<?php
function ListAllHotel()
{
    global $dbsql,$language,$locationid,$html,$cid,$ctype;
    
    TImport('promotions');
    
    $sql_where  = " from tblhotel where cLanguage='$language' and iStatus=1 ";
    if(!empty($cid))
    {
        $sql_where.= " and iParentId in($cid) ";
    }
    else if(intval($locationid)>0)
    {
        $locationIdChilds   = ListIdLocationChild($locationid);
        $listid             = implode(",",$locationIdChilds);
     
        $sql_where.= " and iParentId in($listid) ";
    } 
    if(!empty($ctype))
    {
        $sql_where .= " and ( iTypeId in ($ctype) or iTypeId2 in($ctype) ) ";
    }
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $countall   = $result['count'];
    
    list($lat,$lall)    = Split_Page($countall);
    
    $sql_select = "select iId,iParentId,iTypeId,cName,cUrlString,cImage,cHighLights,iPromotion ".$sql_where;
    $sql_select.= " order by iOrder asc limit $lat,$lall";
    $rs         = $dbsql->query($sql_select);
    $rows       = $dbsql->num_rows($rs);
    $return     = '<div class="ajaxpage">';
    while($result   = $dbsql->fetch_array($rs))
    {
        if($result['iPromotion']==1)
        {
            $promotion  = GetPromotionById($result['iId'],'hotel');
            if($promotion==false)
            {
                $result['iPromotion']=0;
            }
        }
        $link    = '/'.$html->urlRoot. LinkLocationById($result['iParentId']).'/'.$result['cUrlString'].'.htm';
        $return .= '<a class="listtour" href="'.$link.'" title="'.$result['cName'].'">';
        $return .= '<span class="img"><img src="'.GetIntroImg($result['cImage'],true).'" width="155" alt="'.$result['cName'].'" /></span>';
        $return .= '<span class="tt">'.$result['cName'].'</span>';
        if($result['iPromotion']==1)
        {            
            $return .= '<span class="w"><span class="red">'.getLang('Special Offer').": ".$promotion['cPrice'].'</span></span>';
        }else
        {
            $return .= '<span class="w">'.$result['cHighLights'].'</span>';
        }
        $return .= '<span class="l"></span>';
        $return .= '<span class="f">'.getLang('Find out more').'</span>';        
        $return .= '</a>';
    }
    if($rows==0)
    {
        $return .= '<div class="notfound">'.getLang('Not found result!').'</div>';
    }
    $return     .= "<div class='clear'>&nbsp;</div></div>";
    return $return;
}

function ListHotelLocation($id,$hasParent=false)
{
    global $dbsql,$language;
    $id         = intval($id);
    $listcid    = ListIdLocationChild($id);
    if($hasParent)
    {
        $listpid    = ListIdLocationParent($id);
        $listcid    = array_merge($listcid,$listpid);
    }
    
    $listcid    = implode(',',$listcid);

    $sql_select = "select iId,iParentId,cName,cUrlString,cImage,cDescription from tblhotel 
                where iParentId in ($listcid)
                 order by iOrder limit 10";
    $rs         = $dbsql->query($sql_select);
    $hotels     = array();
    while($result   = $dbsql->fetch_array($rs))
    {
        $result['images'] = ListPhotosHotel($result['iId'],2);
        $hotels[]= $result;
    }
    return $hotels;
}

function ListHotelByListLocation($locations)
{
    global $dbsql,$language;
    $listcid    = array(0);
    foreach($locations as $loc)
    {
        $listcid[]= $loc['iId'];
    }
    $listcid    = implode(',',$listcid);

    $sql_select = "select iId,iParentId,cName,cUrlString,cImage,cDescription from tblhotel 
                where iParentId in ($listcid)
                 order by iOrder limit 10";
    $rs         = $dbsql->query($sql_select);
    $hotels     = array();
    while($result   = $dbsql->fetch_array($rs))
    {
        $result['images'] = ListPhotosHotel($result['iId'],2);
        $hotels[]= $result;
    }
    return $hotels;
}

function ListPhotosHotel($id,$limit=0)
{
    global $dbsql,$language;
    
    $id         = intval($id);
    $sql_select = "select cImage,cName from tblhotel_photo where iParentId=$id";
    if($limit>0)
    {
        $sql_select.= " limit $limit";
    }
    return $dbsql->load($sql_select);
}

function getLinkHotel($parentId,$urlhotel)
{
    global $dbsql,$language;
    TImport('location');
    
    $locationurl        = LinkLocationById($parentId);
    
    return getLinkHotelRoot().$locationurl.'/'.$urlhotel.'.htm';    
}

function getLinkHotelRoot()
{
    global $dbsql,$language,$linkhotelroot;
    
    if(!empty($linkhotelroot))
    {
        return $linkhotelroot;
    }
    $sql_select = "select cUrlString from tblcategory where cType='hotel' and cLanguage='$language'";
    $result     = $dbsql->query_fetch($sql_select);
    $linkhotelroot = '/'.$result['cUrlString'];
    return $linkhotelroot;
}

function ListOtherHotel($idtype,$idloc,$id)
{
    global $dbsql,$language;
    
    $id         = intval($id);
    $idtype     = intval($idtype);
    $idloc      = intval($idloc);
    $sql_select = "select cUrlString,cName,cImage,iParentId,cHighLights from tblhotel 
                where (iTypeId=$idtype or iParentId=$idloc) and iId!=$id order by iOrder limit 6";
    return $dbsql->load($sql_select);
}

function ListHotelType()
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cName from tblhotel_type where cLanguage='$language' order by iOrder";
    $rs         = $dbsql->query($sql_select);
    $endline    = '';
    $return     = '';
    while($result   = $dbsql->fetch_array($rs))
    {
        $return .= 'totcb.add({id: '.$result['iId'].',name: "'.$result['cName'].'",g: 0});'.$endline; 
    }
    return $return;
}

function getLinkSendInquiryHotel()
{
    global $dbsql,$language,$html;
    
    $sql_select = "select cUrlString from tblcategory where cType='inquiryhotel' and cLanguage='$language'";
    $result     = $dbsql->query_fetch($sql_select);
    return '/'.$html->urlRoot.'/'.$result['cUrlString'].'/';
}

function InsertBookingHotel()
{
    global $dbsql,$language;
    require_once "includes/php-captcha.inc.php";
    
    if (PhpCaptcha::Validate($_POST['SpamCode'])==false)
    {
         AjaxTransfer('spam',getLang('Please enter the correct security code!'));
    }
    

	$ip              = $_SERVER['REMOTE_ADDR'];
	$datebooking     = date("Y/m/d");
	
	//Personal Details
	$FirstName 		= gpc_getStringPost('FirstName');
	$LastName		= gpc_getStringPost('LastName');
	$FullName		=$FirstName." ".$LastName;
	
	$Email 			= gpc_getStringPost('Email'); 
	$Phone 			= gpc_getStringPost('Phone');
	$Country 		= gpc_getStringPost('Country');
	
	//tour:
    $hotelname      = gpc_getStringPost('hotelname');
 	$arrivalDate 	= gpc_getStringPost('arrivalDate');
 	$departureDate 	= gpc_getStringPost('departureDate');
	
	$Adult 			= gpc_getStringPost('Adult');
 	$Childrent 		= gpc_getStringPost('Childrent');
 	$Infants 		= gpc_getStringPost('Infants');
	
 	$selectr 		= gpc_getStringPost('selectr');
 	$TellUs1 		= gpc_getStringPost('TellUs1');
 	$TellUs2 		= gpc_getStringPost('TellUs2');
 	$Destinations 	= gpc_getStringPost('Destinations');
	$Special 		= gpc_getStringPost('Special');
    $newsletter     = gpc_getBoolPost('newsletter');
    
    ImportClass('country');
    $CountryObj     = new Country();
    $Country        = $CountryObj->getNameByCode($Country);
    
    $emailconfig       = @getConfigEmail('HotelBookingEmail');
    
    if($emailconfig==false)
    {
        AjaxTransfer('config','Email not config');
    }

 	//insert to booking -------------------------------
	$sql_insert = "INSERT INTO tblbookhotel
	(cFName,cLName,cEmail,cPhone,cAddress,cCountry,cArrivalDate,cDepartureDate,
	cAdult,cChildrent,cInfants,cTourName,
	cTellUs1,cTellUs2,cDestinations,cSpecial,iStatus,dDay,cLanguage) 
	VALUES 
	('$FirstName','$LastName','$Email','$Phone','$Address','$Country','$arrivalDate','$departureDate',
	'$Adult','$Childrent','$Infants','$hotelname',
	'$TellUs1','$TellUs2','$Destinations','$Special','0','$datebooking','$language')";
	
	$dbsql->query($sql_insert);	

	//--------------------send to email:------------------------
 	$message_mail="
	<b>I. ".getLang('PERSONAL DETAILS')."</b>
	<div style='padding-left:40'>
		".getLang('Full Name').": ".$FullName ."<br>
		".getLang('Email').": ".$Email."<br>
		".getLang('Phone').": ".$Phone."<br>
		".getLang('Country').": ".$Country."<br>			
		</div><br><br>	
	
	<b>II.  ".getLang('HOTEL INFO')."</b>
	<div style='padding-left:40'>
	".getLang('Hotel Name').": ".$hotelname."<br />    
	".getLang('checkin Date').": ".$arrivalDate."<br />
	".getLang('Departure Date').": ".$departureDate."<br />
	".getLang('Adults').": ". $Adult."<br />
	".getLang('Childs').": ".$Childrent."<br />
	".getLang('Infants').": ".$Infants."<br />
	<div style='padding-left:15px'>".$TellUs1."</div><br />
	".getLang('Destinations').": ".$Destinations."<br /> 	
	".getLang('Special interests').": <div style='padding-left:15px'>".$Special."</div><br>
	<br><br>
 	IP:".$ip;
	
	
    
	$subject               = $emailconfig->title;
    $subject               = str_replace('{HOTELNAME}',$hotelname,$subject);
    $subject               = str_replace('{NAME}',$FullName,$subject);
    $emailconfig->content  = str_replace('{NAME}',$FullName,$emailconfig->content);
    
 	//send to admin
 	$rs   = send_email($emailconfig->email,"",$subject,$message_mail);
 	
 	//send to cus---------------------	    
	$rs   = send_email($Email,"",$subject,$emailconfig->content);     	
	
	//save email newletter--------------------------------------------
	if($newsletter)
 	{
        @ImportClass('newsletter');
 		@regnewsletter($Email,$FullName);
 	}
    
    $datasuccess    = getConfig('BookHotelSuccess');
    $datasuccess    = str_replace('{NAME}',$FullName,$datasuccess);
	AjaxTransfer('ok',$datasuccess);
}
?>